<?php
/**
 * @version      BOSSCMS-Free
 * @trademark    BOSSCMS
 * @copyright    Copyright © by BOSSCMS-Free (https://www.bosscms.net/) 保留所有权利
 * @license      https://gitee.com/Greenpeas/BOSSCMS/license/ 和 https://www.bosscms.net/freelicense/
 * @link         https://www.bosscms.net/
 * @author       温州软界网络技术有限公司
**/
defined('IS_OK') or exit('Access Forbidden');

class page
{
	public static function config($id, $select='*', $where=null, $order=null)
	{
		global $G;
		if(!$where){
			$where = 1;
		}
		if(isset($id)){
			$where = "id='{$id}' AND {$where}";
		}
		if(!isset($order)){
			$order = "id ASC";
		}
		$data = mysql::select_all($select, 'config', $where, $order);
		return $data;
	}
	
	public static function config_option($parent=0, $type=0)
	{
		global $G;
		$data = array();
		$where = 1;
		if(isset($parent)){
			$where = "parent='{$parent}' AND {$where}";
		}
		if(isset($type)){
			$where = "type='{$type}' AND {$where}";
		}
		$result = self::config(null, 'id,name,value', "{$where} AND (lang='{$G['language']['id']}' OR lang=0)");
		$data = arrOption($result);
		if(isset($data['admin_foot'])){
			$data['admin_foot'] = str_replace('[TIME]',date('Y',TIME),$data['admin_foot']);
		}
		if($G['path']['type']=='web'){
			$chars = array(
				'beian',
				'miit_beian',
				'foot',
				'state_content'
			);
			foreach($result as $v){
				if(in_array($v['name'], $chars)){
					$data[$v['name']] = delHtmlspecial($v['value']);
				}
			}
			if(isset($data['logo'])){
				$data['logo_pc'] = $data['logo'];
				if(isMobile()){
					$data['logo'] = $data['logo_mobile'];
				}
			}
		}
		return $data;
	}
	
	
	public static function group($items, $type=null, $select='*', $where=null, $order=null, $limit=null)
	{
		if($sql = self::group_sql($items, $type, $select, $where, $order, $limit)){
			return mysql::select($sql);
		}else{
			return array();
		}
	}

	public static function group_sql($items, $type=null, $select='*', $where=null, $order=null, $limit=null)
	{
		global $G;
		$data = array();
		if(!$where){
			$where = 1;
		}
		if($items){
			$items_list = $items;
			if(is_array($items)){
				$items_list = implode(',',$items);
				$items = array_shift($items);
			}
			if(!$type){
				if($arr = mysql::select_one('type','items',"id='{$items}'")){
					$type = $arr['type'];
				}
			}
			if(is_numeric($type)){
				$table = array_search($type,$G['pass']['type']);
				$itsql = $items_list;
				$res = self::items($items,null,'id');
				foreach($res as $v){
					$itsql .= ','.$v['id'];
				}
				if(strstr($itsql,',')){
					$where .= " AND items IN ({$itsql})";
				}else{
					$where .= " AND items={$itsql}";
				}
			}
		}else if(is_numeric($type)){
			$table = array_search($type,$G['pass']['type']);
		}
		if(!$table || !preg_match('/^(2|3|4|5)$/',$type)){
		    return false;
		}
		if(!isset($order)){
			$order = "top DESC, sort DESC, mtime DESC, id ASC";
		}
		if($tops = mysql::select_all('*','top','etime<='.TIME)){
			foreach($tops as $v){
				mysql::delete('top',"id='{$v['id']}'");
				mysql::update(array('top'=>0),array_search($v['model'],$G['pass']['type']),"id='{$v['parent']}' AND lang=lang");
			}
		}
		return "SELECT {$select} FROM {$G['mysql']['prefix']}{$table} WHERE {$where} ORDER BY {$order}".($limit?" LIMIT {$limit}":'');
	}
	
	public static function group_both($id, $items, $type=null, $number=1, $select='*', $where=null, $order=null, $limit=null)
	{
		global $G;
		$data = array();
		$data['prev'] = array();
		$data['next'] = array();
		if($number<=0){
			return $data;
		}
		if(!$where){
			$where = 1;
		}
		$where = "display=1 AND {$where}";
		if(!$type){
			$arr = mysql::select_one('type','items',"id='{$items}'");
			$type = $arr['type'];
		}
		if($G['config']['both_type']){
			$parent_items = self::items(-$items);
			foreach($parent_items as $v){
				if($v['type'] == $type){
					$items = $v['id'];
				}else{
					break;
				}
			}
		}
		mysql::query('SET @i=0, @n=0');
		$res = mysql::select('SELECT id,@n AS n,i FROM ('.self::group_sql($items, $type, "id,(@i:=@i+1) AS i,(CASE id WHEN {$id} THEN @n:=@i ELSE null END) AS m", $where, $order).') AS t WHERE i>@n-'.($number+1).' LIMIT '.($number*2+1));
		foreach($res as $v){
			if($v['n'] > $v['i']){
				$data['prev'][] = self::group_one($v['id'], $type, $select);
			}else if($v['n'] < $v['i']){
				if($number){
					$data['next'][] = self::group_one($v['id'], $type, $select);
					$number--;
				}
			}
		}
		return $data;
	}
	
	public static function group_pages($items, $type=null, $rows=null, $pages=null, $btns=null, $url=null, $name='pages', $select='*', $where=null, $order=null)
	{
		global $G;
		$data = array();
		if(!$where){
			$where = 1;
		}
		if($G['get']['search'] == 'param'){
			$sql = '';
			preg_match_all("/(?:\?|&)(\w+,\d+)=/", str_replace($name,'',$G['path']['link']), $match);
			if($match[1]){
				foreach($match[1] as $v){
					if(isset($G['get'][$v])){
						$jn = $G['get'][$v]?strFilter(preg_replace('/^\[(.+)\]$/','\\1',json::encode(array(delFilter($G['get'][$v]))))):'';
						$res = mysql::select_all('parent','theme',"name='{$v}' AND ( value='{$G['get'][$v]}' OR LOCATE('{$jn}',value) ) ");
						$s = array();
						foreach($res as $r){
							$s[] = $r['parent'];
						}
						$sql .= " AND id IN (".implode(',',$s).")";
					}
				}
			}
			if($sql){
				$where .= $sql;
			}
		}
		if($items){
			$itemsarr = mysql::select_one('folder,type','items',"id='{$items}'");
			$folder = $itemsarr['folder'];
			$type = $itemsarr['type'];
		}else if(!$type){
			return array();
		}
		$table = array_search($type,$G['pass']['type']);
		if(!$table || !preg_match('/^(2|3|4|5)$/',$type)){
			return array();
		}
		if(!isset($pages)){
			$pages = is_numeric($G['get'][$name])&&$G['get'][$name]>0?ceil($G['get'][$name]):1;
		}
		if(!isset($rows)){
			$rows = is_numeric($G['config'][$table.'_number'])?$G['config'][$table.'_number']:20;
		}
		$data['list'] = self::group($items, $type, "SQL_CALC_FOUND_ROWS *", $where, $order, (($pages-1)*$rows).','.$rows);
		$data['total'] = arrExist(mysql::select('SELECT FOUND_ROWS() AS _total'),'0|_total');
		into::basic_class('pages');
		$data['pages'] = pages::btns(ceil($data['total']/$rows), $pages, $btns, $url, $name);

		if(preg_match('/"^(2|3|4|5)\d$"/',$type)){
			$field = self::field($type);
		}
		$width = $G['config']["{$table}_thumbnail_width"]?$G['config']["{$table}_thumbnail_width"]:300;
		$height = $G['config']["{$table}_thumbnail_height"]?$G['config']["{$table}_thumbnail_height"]:300;
		$complex = array();
		if($data['list']){
			if(strstr($select,'*')){
				$selcon = '*';
			}else{
				$selcon = 'id';
				$selarr = explode(',',$select);
				foreach($selarr as $v){
					$tv = trim($v);
					if($G['database'][$table.'_content'][$tv]){
						$selcon .= ($selcon?',':'').$v;
					}
				}
			}
			if($selcon!='id'){
				$conarr = arrOptions(mysql::select_all($selcon,$table.'_content','id IN ('.implode(',',arrOption($data['list'],'id','id')).')'));
			}
			foreach($data['list'] as $k=>$v){

				if($conarr[$v['id']]){
					$data['list'][$k] = $v = $v+$conarr[$v['id']];
				}
				$data['list'][$k]['width'] = $width;
				$data['list'][$k]['height'] = $height;
				if(!isset($folder) || is_array($items) || $items!=$v['items']){
					$r = mysql::select_one('folder','items',"id='{$v['items']}'");
					$folder = $r['folder'];
				}
				$data['list'][$k]['type'] = $type;
				$data['list'][$k]['folder'] = $folder;
				$data['list'][$k]['url'] = url::group($folder, $v);
				$data['list'][$k]['target'] = $v['target']?'target="_blank"':'';
				if(preg_match('/^(2|3|4|5)$/',$type)){
					$data['list'][$k]['content'] = delHtmlspecial($v['content']);
					if(isset($v['container'])){
						$data['list'][$k]['container'] = delHtmlspecial($v['container']);
					}
				}else{
					foreach($field as $f){
						if($f['style']==2){
							$data['list'][$k][$f['name']] = delHtmlspecial($v[$f['name']]);
						}
					}
				}
				if($type==5){
					if(!$v['size']){
						$data['list'][$k]['size'] = 0;
					}
				}
				if($type!=2 && preg_match('/^(2|3|4|5)$/',$type)){
					if($res = value::get(null,$v['id'],$type,null,false)){
						foreach($res as $n=>$p){
							$data['list'][$k][$n] = preg_match('/^\[".+"\]$/',$p)?implode(',',json::decode($p)):$p;	
						}
						if(preg_match('/^params,(\d+)$/',$n)){
							if(!isset($complex[$v['id']])){
								$complex[$v['id']] = self::complex_list($type, $v['items'], null, 'params');
							}
							foreach($complex[$v['id']] as $c){
								$c['value'] = $data['list'][$k]['params,'.$c['id']];
								$data['list'][$k]['complex'][] = $c;
							}
						}
					}
	
				}
			}
		}
		return $data;
	}

	public static function group_all($rows=null, $pages=null, $btns=null, $url=null, $name='pages', $select='*', $where=null, $order=null)
	{
		global $G;
		if(!$where){
			$where = 1;
		}
		if(!isset($pages)){
			$pages = is_numeric($G['get'][$name])&&$G['get'][$name]>0?ceil($G['get'][$name]):1;
		}
		if(!isset($rows)){
			$rows = 20;
		}
		if(!isset($order)){
			$order = "top DESC, sort DESC, mtime DESC, id ASC";
		}
		$sql = array();
		$field = preg_replace('/(,?\s*id)?\s+(DESC|ASC)/i','',$order);
		foreach($G['database']['news'] as $k=>$v){
			if(preg_match("/\W{$k}\W/"," {$where} ") && !strstr($field,$k)){
				$field .= ','.$k;
			}
		}
		foreach($G['pass']['type'] as $k=>$v){
			if(preg_match('/^(2|3|4|5)$/',$v)){
				$sql[] = "SELECT id,{$field},{$v} AS type FROM ".$G['mysql']['prefix'].$k." WHERE lang={$G['language']['id']}";
			}
		}
		if($tops = mysql::select_all('*','top','etime<='.TIME)){
			foreach($tops as $v){
				mysql::delete('top',"id='{$v['id']}'");
				mysql::update(array('top'=>0),array_search($v['model'],$G['pass']['type']),"id='{$v['parent']}' AND lang=lang");
			}
		}
		$data['list'] = mysql::select("SELECT SQL_CALC_FOUND_ROWS * FROM (".implode(' UNION ALL ',$sql).") AS t WHERE {$where} ORDER BY {$order} LIMIT ".(($pages-1)*$rows).','.$rows);
		$data['total'] = arrExist(mysql::select('SELECT FOUND_ROWS() AS _total'),'0|_total');
		$arr = array();
		foreach($data['list'] as $v){
			$arr[$v['type']][] = $v['id'];
		}
		foreach($arr as $k=>$v){
			$table = array_search($k,$G['pass']['type']);
			$list = arrOptions(mysql::select_all($select,$table.'_content','id IN ('.implode(',',$v).')'),'id');
			$res = mysql::select_all('*',$table,'id IN ('.implode(',',$v).')');
			$arr[$k] = array();
			foreach($res as $v2){
				$v2 = $v2+array('type'=>$k);
				$li = $list[$v2['id']];
				$arr[$k][$v2['id']] = $li?$v2+$li:$v2;
			}
		}
		foreach($data['list'] as $k=>$v){
			$data['list'][$k] = $v = $arr[$v['type']][$v['id']];
			if(!$folder = $G['items_list'][$v['items']]){
				$folder = $G['items_list'][$v['items']] = arrExist(mysql::select_one('folder','items',"id='{$v['items']}'"),'folder');
			}
			$data['list'][$k]['url'] = url::group($folder,$v);
		}
		into::basic_class('pages');
		$data['pages'] = pages::btns(ceil($data['total']/$rows), $pages, null, null, 'pages');
		return $data;
	}
	
	public static function group_list($items=null, $rows=null, $pages=null, $btns=null, $url=null, $name='pages', $select='*', $where=null, $order=null)
	{
		global $G;
		if(!$where){
			$where = 1;
		}
		if(!isset($items)){
			$items = $G['items']['id'];
		}
		if(!isset($url) && $G['config']['rewrite_open'] && $G['get']['search']!='param'){
			$itemsarr = mysql::select_one('*','items',"id='{$items}'");
			$url = url::items($itemsarr,null,null,null,false);
		}
		$where = "display=1 AND {$where}";
		$data = self::group_pages($items, null, $rows, $pages, $btns, $url, $name, $select, $where, $order);
		return $data;
	}

	public static function group_tag($tag=null, $type=null, $rows=null, $pages=null, $btns=null, $url=null, $name='pages', $select='*', $where=null, $order=null)
	{
		global $G;
		if(!$where){
			$where = 1;
		}
		if(!isset($tag)){
			$tag = $G['get']['tag'];
		}
		$res = self::tag_name($tag,'parent');	
		if(!isset($type)){
			$type = $G['items']['type'];
		}
		if(!isset($url) && $G['config']['rewrite_open'] && $G['get']['search']!='param'){
			$itemsarr = mysql::select_one('folder','items',"type='{$type}'",'sort DESC, id ASC');
			$url = url::tag($itemsarr['folder'],array('name'=>$tag),null,null,null,false);
		}
		$where = "FIND_IN_SET(id,'{$res['parent']}') AND display=1 AND {$where}";
		$data = self::group_pages(null, $type, $rows, $pages, $btns, $url, $name, $select, $where, $order);
		return $data;
	}
	
	public static function group_type($type, $rows=null, $pages=null, $btns=null, $url=null, $name='pages', $select='*', $where=null, $order=null)
	{
		global $G;
		if(!$where){
			$where = 1;
		}
		if(!isset($url) && $G['config']['rewrite_open'] && $G['get']['search']!='param'){
			$itemsarr = mysql::select_one('folder','items',"type='{$type}'",'sort DESC, id ASC');
			$url = url::items($itemsarr,null,null,null,false);
		}
		$where = "display=1 AND {$where}";
		$data = self::group_pages(null, $type, $rows, $pages, $btns, $url, $name, $select, $where, $order);
		return $data;
	}

	public static function group_one($id, $type, $select='*', $where=null, $order=null)
	{
		global $G;
		if(!$where){
			$where = 1;
		}
		$table = array_search($type,$G['pass']['type']);
		if(!$table || !preg_match('/^(2|3|4|5)$/',$type)){
			return array();
		}
		if(is_numeric($id)){
			$where = "id='{$id}' AND {$where}";
		}
		$where = "display=1 AND {$where}";
		if(!isset($order)){
			$order = "top DESC, sort DESC, mtime DESC, id ASC";
		}
		$data = mysql::select_one('*', $table, $where, $order);
		$data['width'] = $G['config']["{$table}_thumbnail_width"];
		$data['height'] = $G['config']["{$table}_thumbnail_height"];
		if(isset($data['id'])){
			if($res = mysql::select_one($select, $table.'_content', "id='{$data['id']}'")){
				$data = $data+$res;
			}
			if($type==3){
				for($i=0; $i<$G['config']['product_content_number']; $i++){
					$k=$i?$i:'';
					if($k){
						$data['content'.$k] = delHtmlspecial($data['content'.$k]);
					}
					$data['contents'][$i] = array(
						'title' => $G['config']['product_content_title'.$k],
						'content' => delHtmlspecial($data['content'.$k])
					);
				}
			}else if($type==5){
				if(!$data['size']){
					$data['size'] = 0;
				}
			}
			if($type!=2){
				if($res = value::get(null,$data['id'],$type,null,false)){
					foreach($res as $k=>$v){
						$data[$k] = preg_match('/^\[".+"\]$/',$v)?implode(',',json::decode($v)):$v;
					}
					if(preg_match('/^params,(\d+)$/',$k)){
						$comp = self::complex_list($type, $data['items'], null, 'params');
						foreach($comp as $c){
							$c['value'] = $data['params,'.$c['id']];
							$data['complex'][] = $c;
						}
					}
				}
			}
		}
		$res = mysql::select_one('folder','items',"id='{$data['items']}'");
		if($images = arrExist($data,'images')){
			$data['imgs'] = json::decode($images);
		}else{
			if($image = arrExist($data,'image')){
				$data['imgs'][0] = $image;
			}else{
				$data['imgs'] = array();
			}
		}
		$data['tag'] = self::tag_list($type, $id);
		$data['folder'] = $res['folder'];
		$data['url'] = url::group($res['folder'], $data);
		$data['target'] = $data['target']?'target="_blank"':'';
		$data['type'] = $type;
		if(preg_match('/^(2|3|4|5)$/',$type)){
			$data['content'] = delHtmlspecial($data['content']);	
			if(isset($data['container'])){
				$data['container'] = delHtmlspecial($data['container']);
			}
		}else{
			$field = self::field($type);
			foreach($field as $v){
				if($v['style']==2){
					$data[$v['name']] = delHtmlspecial($data[$v['name']]);
				}
			}
		}
		return $data;
	}

	
	public static function tag_lists($type=null, $parent=null, $rows=null, $pages=null, $btns=null, $url=null, $name='pages', $select='*', $where=null, $order=null)
	{
		global $G;
		if(!isset($rows)){
			$rows = is_numeric($G['config']['tag_number'])?$G['config']['tag_number']:100;
		}
		if(!isset($pages)){
			$pages = is_numeric($G['get'][$name])&&$G['get'][$name]>0?ceil($G['get'][$name]):1;
		}
		$data['list'] = self::tag_list($type, $parent, 'SQL_CALC_FOUND_ROWS '.$select, $where, $order, (($pages-1)*$rows).','.$rows);
		$data['total'] = arrExist(mysql::select('SELECT FOUND_ROWS() AS _total'),'0|_total');
		into::basic_class('pages');
		if(!isset($url) && $G['config']['rewrite_open']){
			$itemsarr = mysql::select_one('*','items',"id='{$G['items']['id']}'");
			$url = url::items($itemsarr,null,null,null,false);
		}
		$data['pages'] = pages::btns(ceil($data['total']/$rows), $pages, $btns, $url, $name);
		return $data;
	}

	public static function tag_list($type=null, $parent=null, $select='*', $where=null, $order=null, $limit=null)
	{
		global $G;
		if(!$where){
			$where = 1;
		}
		$folder = array();
		if(isset($type)){
			$res = mysql::select_one('folder','items',"type='{$type}'",'sort DESC, id ASC');
			$folder[$type] = $res['folder'];
		}else{
			foreach(array(2,3,4,5) as $v){
				$res = mysql::select_one('folder','items',"type='{$v}'",'sort DESC, id ASC');
				$folder[$v] = $res['folder'];
			}
		}
		$data = self::tag($type, $parent, $select, $where, $order, $limit);
		foreach($data as $k=>$v){
			$data[$k]['url'] = url::tag($folder[$v['type']], $v);
		}
		return $data;
	}
	
	public static function tag_name($name, $select='*', $where=null, $order=null)
	{
		if(!$where){
			$where = 1;
		}
		if(is_numeric($name)){
			$data = self::tag_one($name, $select, $where, $order);
		}else{
			$where = "name='{$name}' AND {$where}";
			$data = self::tag_one(null, $select, $where, $order);
		}
		return $data;
	}
	
	public static function tag_one($id=null, $select='*', $where=null, $order=null)
	{
		if(!$where){
			$where = 1;
		}
		if(isset($id)){
			$where = "id='{$id}'";
		}
		if(!isset($order)){
			$order = "id ASC";
		}
		$data = mysql::select_one($select, 'tag', $where, $order);
		return $data;
	}
	
	public static function tag_pages($type=null, $parent=null, $rows=null, $pages=null, $btns=null, $url=null, $name='pages', $select='*', $where=null, $order=null)
	{
		global $G;
		$data = array();
		if(!isset($rows)){
			$rows = 20;
		}
		$res = self::tag($type, $parent, 'COUNT(*) AS _total', $where);
		$data['total'] = $res[0]['_total'];
		into::basic_class('pages');
		$data['pages'] = pages::btns(ceil($data['total']/$rows), $pages, $btns, $url, $name);
		$pages = $data['pages'][$name];
		$start = ($pages-1) * $rows;
		$data['list'] = self::tag($type, $parent, $select, $where, $order, "{$start},{$rows}");
		return $data;
	}
	
	public static function tag($type, $parent, $select='*', $where=null, $order=null, $limit=null)
	{
		if(!$where){
			$where = 1;
		}
		if(isset($type)){
			$where = "type='{$type}' AND {$where}";
		}
		if(isset($parent)){
			$where = "FIND_IN_SET('{$parent}',parent) AND {$where}";
		}
		if(!isset($order)){
			$order = "id ASC";
		}
		$data = mysql::select_all($select, 'tag', $where, $order, $limit);
		return $data;
	}
	
    public static function form($id, $where=null, $order=null)
	{
		global $G;
		$data = array();
		if(!$where){
			$where = 1;
		}
		if(isset($id)){
			$where = "type=0 AND parent='{$id}' AND {$where}";
		}
		if(!isset($order)){
			$order = 'sort ASC, id ASC';
		}
		$result = mysql::select_all('*', 'form', $where, $order);
		foreach($result as $k=>$v){
			$data[$k] = $v;
			$data[$k]['form'] = ctrl::form($v['style'],'params'.$v['id'],$v['param'],$v['must'],$v['prompt']);
		}
		return $data;
	}

	public static function feedback($items, $rows=null, $pages=null, $btns=null, $url=null, $name='pages', $select='*', $where=null, $order=null)
	{
		global $G;
		$data = array();
		if(!$where){
			$where = 1;
		}
		if($items){
			$where = "parent='{$items}' AND {$where}";
		}
		if(!isset($pages)){
			$pages = is_numeric($G['get'][$name])&&$G['get'][$name]>0?ceil($G['get'][$name]):1;
		}
		if(!isset($rows)){
			$rows = $G['config']['feedback_number'];
		}
		if(!isset($order)){
			$order = 'ctime DESC, id ASC';
		}
		$data['list'] = mysql::select_all('SQL_CALC_FOUND_ROWS '.$select, 'feedback', $where, $order, (($pages-1)*$rows).','.$rows);
		foreach($data['list'] as $k=>$v){
			$param = json::decode($v['param']);
			foreach($param as $k2=>$v2){
				$param[$k2] = is_array($v2)?implode(', ',$v2):$v2;
			}
			$data['list'][$k]['param'] = $param;
		}
		$data['total'] = arrExist(mysql::select('SELECT FOUND_ROWS() AS _total'),'0|_total');
		into::basic_class('pages');
		if(!isset($url) && $G['path']['type']=='web' && $G['config']['rewrite_open']){
			$itemsarr = mysql::select_one('*','items',"id='{$items}'");
			$url = url::items($itemsarr,null,null,null,false);
		}
		$data['pages'] = pages::btns(ceil($data['total']/$rows), $pages, $btns, $url, $name);
		return $data;
	}

	public static function feedback_list($items, $rows=null, $pages=null, $btns=null, $url=null, $name='pages', $select='*', $where=null, $order=null)
	{
		if(!$where){
			$where = 1;
		}
		$res = mysql::select_one('value','config',"name='feedback_display' AND parent='{$items}'");
		$where = $res['value']?"display=1 AND {$where}":' 0 ';
		return self::feedback($items, $rows, $pages, $btns, $url, $name, $select, $where, $order);
	}
	

	public static function plugin_list($select='*', $where=null, $order=null)
	{
		global $G;
		if(!$where){
			$where = 1;
		}
		$where = "display=1 AND {$where}";
		$data = self::plugin($select, $where, $order);
		foreach($data as $k=>$v){
            $data[$k]['target'] = $v['target']?' target="_blank"':'';
		}
		return $data;
	}
    
	public static function plugin($select='*', $where=null, $order=null)
	{
		if(!$where){
			$where = 1;
		}
		if(!isset($order)){
			$order = "sort DESC, id ASC";
		}
		$data = mysql::select_all($select, 'plugin', $where, $order);
		return $data;
	}
	
	public static function language_one($id, $select='*', $where=null, $order=null)
	{
		global $G;
		if(!$where){
			$where = 1;
		}
		if(is_numeric($id)){
			$where = "id='{$id}' AND {$where}";
		}
		$where = "display=1 AND {$where}";
		$data = mysql::select_one($select, 'language', $where, $order);
		$data['url'] = url::home($data['id']);
		$data['target'] = $data['target']?' target="_blank"':'';
		return $data;
	}
	
	public static function language_list($select='*', $where=null, $order=null)
	{
		global $G;
		if(!$where){
			$where = 1;
		}
		$where = "display=1 AND {$where}";
		$data = self::language($select, $where, $order);
		foreach($data as $k=>$v){
			$data[$k]['url'] = url::home($v['id']);
            $data[$k]['target'] = $v['target']?' target="_blank"':'';
		}
		return $data;
	}

	public static function language($select='*', $where=null, $order=null)
	{
		if(!$where){
			$where = 1;
		}
		if(!isset($order)){
			$order = "sort DESC, id ASC";
		}
		$data = mysql::select_all($select, 'language', $where, $order);
		return $data;
	}
	
	
	public static function banner_list($items=null, $type=null, $select='*', $where=null, $order=null)
	{
		global $G;
		if(!$where){
			$where = 1;
		}
		if(!isset($type)){
			$type = $G['items']['type'];
		}
		if(!isset($items)){
			$items = $G['items']['id'];
		}
		$screen = isMobile()?1:0;
		$where = "((type!='' AND LOCATE('\"{$type}\"',type)>0) OR (items!='' AND LOCATE('\"{$items}\"',items)>0)) AND screen='{$screen}' AND display=1 AND {$where}";
		$data = self::banner($select, $where, $order);
		foreach($data as $k=>$v){
			$data[$k]['content'] = delHtmlspecial($v['content']);
            $data[$k]['target'] = $v['target']?' target="_blank"':'';
		}
		return $data;
	}
	public static function banner_pages($rows=null, $pages=null, $btns=null, $url=null, $name='pages', $select='*', $where=null, $order=null)
	{
		global $G;
		$data = array();
		if(!isset($pages)){
			$pages = is_numeric($G['get'][$name])&&$G['get'][$name]>0?ceil($G['get'][$name]):1;
		}
		if(!isset($rows)){
			$rows = 20;
		}
		$data['list'] = self::banner('SQL_CALC_FOUND_ROWS '.$select, $where, $order, (($pages-1)*$rows).','.$rows);
		$data['total'] = arrExist(mysql::select('SELECT FOUND_ROWS() AS _total'),'0|_total');
		into::basic_class('pages');
		$data['pages'] = pages::btns(ceil($data['total']/$rows), $pages, $btns, $url, $name);
		return $data;
	}
	public static function banner($select='*', $where=null, $order=null, $limit=null)
	{
		if(!$where){
			$where = 1;
		}
		if(!isset($order)){
			$order = "sort DESC, id ASC";
		}
		$data = mysql::select_all($select, 'banner', $where, $order, $limit);
		return $data;
	}
	
	
	public static function link_list($items=null, $type=null, $select='*', $where=null, $order=null)
	{
		global $G;
		if(!$where){
			$where = 1;
		}
		if(isset($type)){
			$where .= " AND type='{$type}' ";
		}
		if(!$items){
			$items = $G['items']['id'];
		}
		$where .= " AND LOCATE('\"{$items}\"',items)>0";
		$where = "display=1 AND {$where}";
		$data = self::link($select, $where, $order);
		foreach($data as $k=>$v){
            $data[$k]['nofollow'] = $v['nofollow']?' rel="nofollow"':'';
            $data[$k]['target'] = $v['target']?' target="_blank"':'';
		}
		return $data;
	}
	public static function link_pages($rows=null, $pages=null, $btns=null, $url=null, $name='pages', $select='*', $where=null, $order=null)
	{
		global $G;
		$data = array();
		if(!isset($pages)){
			$pages = is_numeric($G['get'][$name])&&$G['get'][$name]>0?ceil($G['get'][$name]):1;
		}
		if(!isset($rows)){
			$rows = 20;
		}
		$data['list'] = self::link('SQL_CALC_FOUND_ROWS '.$select, $where, $order, (($pages-1)*$rows).','.$rows);
		$data['total'] = arrExist(mysql::select('SELECT FOUND_ROWS() AS _total'),'0|_total');
		into::basic_class('pages');
		$data['pages'] = pages::btns(ceil($data['total']/$rows), $pages, $btns, $url, $name);
		return $data;
	}
	public static function link($select='*', $where=null, $order=null, $limit=null)
	{
		if(!$where){
			$where = 1;
		}
		if(!isset($order)){
			$order = "sort DESC, id ASC";
		}
		$data = mysql::select_all($select, 'link', $where, $order, $limit);
		return $data;
	}

	public static function layers_series($core, $series='', $parent=0, $level=1)
	{
		$data = array();
		if($series){
			$list = self::layers($core, $series, $parent);
			foreach($list as $key=>$val){
				$val['level'] = $level;
				$data[] = $val;
				$result = theme::series_list($core, 88, $series);
				foreach($result as $k=>$v){
					$v['level'] = $level+1;
					$v['id'] = $k;
					$v['series'] = $series;
					$data[] = $v;
					$res = self::layers_series($core, $k, $val['id'], $v['level']+1);
					$data = array_merge($data, $res);
				}
			}
		}else{
			$result = theme::series_list($core, 88, $series);
			foreach($result as $k=>$v){
				$v['level'] = $level;
				$v['id'] = $k;
				$v['series'] = $series;
				$data[] = $v;
				$res = self::layers_series($core, $k, $parent, $v['level']+1);
				$data = array_merge($data, $res);
			}
		}
		return $data;
	}
	
	public static function layers_list($core, $series=1, $parent=0, $select='*', $where=null, $order=null)
	{
		global $G;
		if(!$where){
			$where = 1;
		}
		$where = "display=1 AND {$where}";
		$data = self::layers($core, $series, $parent, $select, $where, $order);
		foreach($data as $k=>$v){
			$value = value::get($core, $v['id'], 88, $series);
			foreach($value as $name=>$info){
				$data[$k][$name] = $info;
			}
		}
		return $data;
	}
	
	public static function layers($core, $series=1, $parent=0, $select='*', $where=null, $order=null)
	{
		if(!$where){
			$where = 1;
		}
		$where = "parent='{$parent}' AND core='{$core}' AND series='{$series}' AND {$where}";
		if(!isset($order)){
			$order = "sort DESC, id ASC";
		}
		$data = mysql::select_all($select, 'layers', $where, $order);
		return $data;
	}
	

    public static function complex($extent, $items, $core, $name, $parent=0)
	{
		global $G;
		/* BOSS—CMS */
		if(preg_match('/^(2|3|4|5)$/',$extent)){
			$sql = "FIND_IN_SET(items,'{$items}";
			$res = self::items(-$items, null, 'id', "type='{$extent}'");
			foreach($res as $v){
				$sql .= ','.$v['id'];
			}
			$sql .= "')";
		}else{
			$sql = "items='{$items}'";
		}
		$where = "extent='{$extent}' AND core='{$core}' AND name='{$name}' AND parent='{$parent}' AND {$sql}";
		$order = 'sort ASC, id ASC';
		$data = mysql::select_all('*', 'complex', $where, $order);
		return $data;
	}

    public static function complex_list($extent, $items, $core, $name, $parent=0)
	{
		global $G;		
		$data = self::complex($extent, $items, $core, $name, $parent);
		return $data;
	}

	public static function complex_one($id=null, $select='*', $where=null)
	{
		global $G;
		if(!$where){
			$where = 1;
		}
		if(isset($id)){
			$where = "id='{$id}' AND {$where}";
		}
		$data = mysql::select_one($select, 'complex', $where);
		return $data;
	}
	
	public static function search_group($items, $rows=null, $pages=null, $btns=null, $url=null, $name='pages', $select='*', $where=null, $order=null)
	{
		global $G;
		$data = array();
		if(!$where){
			$where = 1;
		}
		if($items){
			$where = "parent='{$items}' AND {$where}";
		}
		if(!isset($rows)){
			$rows = 20;
		}
		if(!isset($pages)){
			$pages = is_numeric($G['get']['pages'])&&$G['get']['pages']>0?ceil($G['get']['pages']):1;
		}
		if(!isset($order)){
			$order = "ctime DESC, id ASC";
		}
		$data['list'] = mysql::select("SELECT SQL_CALC_FOUND_ROWS SUBSTRING_INDEX(SUBSTRING_INDEX(c,'&',1),'&',-1) AS id,SUBSTRING_INDEX(SUBSTRING_INDEX(c,'&',2),'&',-1) AS parent,keyword,SUBSTRING_INDEX(SUBSTRING_INDEX(c,'&',3),'&',-1) AS ip,SUBSTRING_INDEX(SUBSTRING_INDEX(c,'&',4),'&',-1) AS ctime,SUBSTRING_INDEX(SUBSTRING_INDEX(c,'&',5),'&',-1) AS lang,total FROM (SELECT keyword,MAX(ctime) AS ctime,GROUP_CONCAT(id,'&',parent,'&',ip,'&',ctime,'&',lang ORDER BY ctime DESC SEPARATOR '&') AS c,COUNT(*) AS total FROM {$G['mysql']['prefix']}search WHERE {$where} GROUP BY keyword) AS t ORDER BY {$order} LIMIT ".(($pages-1)*$rows).','.$rows."");
		$data['total'] = arrExist(mysql::select('SELECT FOUND_ROWS() AS _total'),'0|_total');
		into::basic_class('pages');
		$data['pages'] = pages::btns(ceil($data['total']/$rows), $pages, $btns, $url, $name);
		return $data;
	}

	public static function search($items, $rows=null, $pages=null, $btns=null, $url=null, $name='pages', $select='*', $where=null, $order=null)
	{
		global $G;
		$data = array();
		if(!$where){
			$where = 1;
		}
		if($items){
			$where = "parent='{$items}' AND {$where}";
		}
		if(!isset($rows)){
			$rows = 20;
		}
		if(!isset($pages)){
			$pages = is_numeric($G['get']['pages'])&&$G['get']['pages']>0?ceil($G['get']['pages']):1;
		}
		if(!isset($order)){
			$order = "ctime DESC, id ASC";
		}
		$data['list'] = mysql::select_all('SQL_CALC_FOUND_ROWS '.$select, 'search', $where, $order, (($pages-1)*$rows).','.$rows);
		$data['total'] = arrExist(mysql::select('SELECT FOUND_ROWS() AS _total'),'0|_total');
		into::basic_class('pages');
		$data['pages'] = pages::btns(ceil($data['total']/$rows), $pages, $btns, $url, $name);
		return $data;
	}

	public static function search_keyword($items, $keyword, $rows=null, $pages=null, $btns=null, $url=null, $name='pages', $select='*', $where=null, $order=null)
	{
		global $G;
		$data = array();
		if(!$where){
			$where = 1;
		}
		if($keyword){
			$where = "keyword='{$keyword}' AND {$where}";
		}
		return self::search($items, $rows, $pages, $btns, $url, $name, $select, $where, $order);
	}
	
	public static function search_list($items=null, $keyword=null, $rows=null, $pages=null, $btns=null, $url=null, $name='pages')
	{
		global $G;
		$data = array();
		if(!isset($items)){
			$items = $G['items']['id'];
		}
		$config = self::config_option($items,0);
		if(!isset($keyword)){
			$keyword = arrExist($G,"get|{$config['search_keyword']}");
		}
		if($keyword!=='' && arrExist($config,'search_open') && $search_items=json::decode($config['search_items'])){
			$sql = $tp = $fo = array();
			$its = mysql::select_all('*','items',"FIND_IN_SET(id,'".implode(',',$search_items)."')",'type ASC,id ASC');
			foreach($its as $v){
				$fo[$v['id']] = $v;
				$tp[$v['type']][] = $v['id'];
			}
			foreach($tp as $k=>$v){
				if($k==1){
					$sql[] = "SELECT id,name AS title,9999999999 AS time,image,content AS text,type,0 AS items FROM {$G['mysql']['prefix']}items WHERE FIND_IN_SET(id,'".implode(',',$v)."') AND display=1 AND lang='{$G['language']['id']}' AND (name LIKE '%{$keyword}%' OR text LIKE '%{$keyword}%' OR content LIKE '%{$keyword}%')";
				}else if(preg_match('/^(2|3|4|5)$/',$k) && $table=array_search($k,$G['pass']['type'])){
					$write = "SELECT t1.id,t1.name AS title,t1.ctime AS time,'' AS image,'' AS text,{$k} AS type,t1.items FROM {$G['mysql']['prefix']}{$table} AS t1 INNER JOIN {$G['mysql']['prefix']}{$table}_content AS t2 ON t1.id=t2.id AND FIND_IN_SET(t1.items,'".implode(',',$v)."') AND t1.display=1 AND t1.lang='{$G['language']['id']}' AND (t1.name LIKE '%{$keyword}%' OR t2.content LIKE '%{$keyword}%')";				
					$sql[] = $write;
				}
			}
			if(!isset($pages)){
				$pages = is_numeric($G['get']['pages'])&&$G['get']['pages']>0?ceil($G['get']['pages']):1;
			}
			if(!isset($rows)){
				$rows = is_numeric($G['config']['search_number'])?$G['config']['search_number']:20;
			}
			$data['list'] = mysql::select('SELECT SQL_CALC_FOUND_ROWS * FROM ('.implode(' UNION ALL ',$sql).') AS t ORDER BY time DESC LIMIT '.(($pages-1)*$rows).','.$rows);
			$res = mysql::select('SELECT FOUND_ROWS() AS _total');
			$data['total'] = $res[0]['_total'];
			into::basic_class('pages');
			$data['pages'] = pages::btns(ceil($data['total']/$rows), $pages, $btns, $url, $name);
			foreach($data['list'] as $k=>$v){
				if(preg_match('/^(2|3|4|5)$/',$v['type'])){
					$r = mysql::select_one('*',array_search($v['type'],$G['pass']['type']).'_content',"id='{$v['id']}'");
					$v['image'] = $r['image'];
					$v['text'] = $r['text'];
					$data['list'][$k] = $v = $v+$r;
				}
				$data['list'][$k]['time'] = $v['type']==1?0:$v['time'];
				$data['list'][$k]['url'] = $v['type']==1?url::items($fo[$v['id']]):url::group($fo[$v['items']]['folder'],$v);
				$data['list'][$k]['text'] = delHtmlspecial($v['text']);
			}
		}
		return $data;
	}
	
	
	public static function search_param($items)
	{
		global $G;
		$data = array();
		$arr = mysql::select_one('type','items',"id='{$items}'");
		$result = self::complex_list($arr['type'], $items, null, 'params');
		foreach($result as $v){
			if($v['style']==3 || $v['style']==4 || $v['style']==5){
				$name = $v['name'].','.$v['id'];
				$data[$name]['title'] = $v['title'];
				preg_match("/\w+,\d+=/", str_replace($name,'',$G['path']['link']), $match);
				$data[$name]['list'][] = array(
					'url' =>  url::param(url::param(null, 'search',  $match[0]?'param':null), $name, null),
					'title' => $G['config']['all'],
					'active' => isset($G['get'][$name])?false:true,
					'all' => true
				);
				$rgb = false;
				$param = json::decode($v['param']);
				foreach($param as $k=>$m){
					preg_match('/^rgb[a]*\([\d,\s]*+\)[;]*$/', $m, $match);
					if($match[0]){
						$rgb = true;
					}
					$data[$name]['list'][] = array(
						'url' =>  url::param(url::param(null,'search','param'), $name, $m),
						'title' => $m,
						'rgb' => $match[0]?true:false,
						'active' => arrExist($G['get'],$name)==$m?true:false,
						'all' => false
					);
				}
				$data[$name]['rgb'] = $rgb;
			}
		}
		return $data;
	}

	public static function items_one($id=0, $select='*', $where=true, $order=null)
	{
		global $G;
		$items = arrExist($G,'items|id');
		if($id == 88888){
			$data = array(
				'id' => $id,
				'name' => $G['config']['home'],
				'type' => 0,
				'theme' => 'home.html',
				'parent' => 0,
				'display' => 1,
				'target' => '',
				'text' => '',
				'level' => 1,
				'static' => '',
				'children' => 0,
				'url' => url::home(),
				'on' => $G['path']['home']?'on':''
			);
		}else{
			$sql = $id?"id='{$id}'":"1";
			$data = isset($G['items'])&&is_numeric($items)&&$id==$items?$G['items']:(mysql::select_one($select, 'items', "{$sql} AND display=1 AND {$where}", $order));
		}
		if($data && $id!=88888){
			$data['on'] = $id==$items?'on':'';
			$data['url'] = url::items($data);
			$data['children'] = mysql::total('items', "parent='{$data['id']}' AND display=1");
			$data['target'] = ($data['target']?'target="_blank"':'').($data['nofollow']?' rel="nofollow"':'');
			$data['content'] = delHtmlspecial($data['content']);
			if(isset($data['container'])){
				$data['container'] = delHtmlspecial($data['container']);
			}
		}
		return $data;
	}
	
	public static function items_head()
	{
		return self::items_list(null, 0, '*', "head=1");
	}
	
	public static function items_foot()
	{
		return self::items_list(null, 0, '*', "foot=1");
	}

	public static function items_list($id=0, $loop=0, $select='*', $where=true, $order=null)
	{
		global $G;
		$nowid = arrExist($G,'items|id');
		$parents = array();
		if(isset($G['items']['parent_list'])){
			foreach($G['items']['parent_list'] as $k=>$v){
				$parents[] = $v['id'];
			}
		}
		$data = self::items($id, $loop, $select, "display=1 AND {$where}", $order);
		if($id < 0){
			$data = array_reverse($data);
		}
		foreach($data as $k=>$v){
			$data[$k]['on'] = $v['id']==$nowid?'on':($parents&&in_array($v['id'],$parents)?'on pn':'');
			$data[$k]['url'] = url::items($v);
			$data[$k]['children'] = mysql::total('items', "parent='{$v['id']}' AND display=1");
			$data[$k]['target'] = ($v['target']?'target="_blank"':'').($v['nofollow']?' rel="nofollow"':'');			
			$data[$k]['content'] = delHtmlspecial($v['content']);
			if(isset($v['container'])){
				$data[$k]['container'] = delHtmlspecial($v['container']);
			}
		}
		return $data;
	}
	
	public static function items_option($id=0, $top=false, $over=array(), $home=false, $type=null)
	{
		global $G;
		$data = array();
		if($top){
			$data[0] = $top;
		}
		$where = ' 1';
		if($over){
			$over = implode(',',$over);
			$where .= " AND !FIND_IN_SET(parent,'{$over}') AND !FIND_IN_SET(id,'{$over}')";
		}
		if($home){
			$data[88888] = $G['config']['home'];
		}
		$subset = self::items($id, null, 'id,level,name,parent,type', $where);
		if($subset){
			foreach($subset as $v){
				if(!$type || (is_array($type)?in_array($v['type'],$type):preg_match('/^'.$type.'$/',$v['type']))){ 
					$r = '';
					for($i=($top?1:2); $i<=$v['level']; $i++){
						if($i==$v['level']) $r.='<font>&nbsp;&nbsp;&#10551;&nbsp;</font>'; else $r.='<font>&emsp;&nbsp;</font>';
					}
					$data[$v['id']] = $r.$v['name'];
				}
			}
		}
		return $data;
	}
	
	public static function items($parent=0, $loop=null, $select='*', $where=true, $order=null)
	{
		/* BOSS_CMS */
		$data = array();
		if(isset($parent) && is_numeric($parent)){
			if($parent<0){
				$set = " id='".abs($parent)."' AND ";
			}else{
				$set = " parent='{$parent}' AND ";
			}
		}
		if(!strstr($select,'parent')){
			$select .= ',parent,id';
		}
		if(is_numeric($loop)){
			$loop = $loop-1;
		}
		if(!isset($order)){
			$order = 'sort DESC, id ASC';
		}
		if($result = mysql::select_all($select, 'items', $set.$where, $order)){
			foreach($result as $val){
				$data[$val['id']] = $val;
				if((!isset($loop) || $loop>=0) && ($parent>=0 || ($parent<0&&$val['parent'])) && $res=self::items($parent<0?-$val['parent']:$val['id'], $loop, $select, $where, $order)){
					foreach($res as $v){
						$data[$v['id']] = $v;
					}
				}
			}
			return $data;
		}
		return array();
	}
}
?>